Why does that happen? How do I prevent it from happening?
— Kevan
Hi Kevan. We have seen this before…
You’re encountering a Race Condition as Windows shuts downUnfortunately you are experiencing the effects of the chaotic and poorly documented Windows shutdown process.
From the timeline you have provided, you initiated the reboot at about 10:26 AM, right? Within a few seconds of that, Windows logs off the user signed in to Session 1.
That system-initiated logoff terminates Session 1. Windows stops all programs running in Session 1 — forcibly if necessary. And since your application is running in Session 1, it’s terminated too. That happens at about 10:26:08.
AlwaysUp, which is constantly monitoring your application in the background, immediately notices that the Session is closed and that your application has stopped. AlwaysUp writes an informative log entry at 10:26:08:
Next, AlwaysUp springs into action to ensure that your application runs 24/7. It knows your preference is to run in Session 1, but that’s no longer possible. The only reliable place to restart your application is in the session that is always available to Windows Services — the isolated Session 0.
So AlwaysUp starts your application in Session 0. The log line at 10:26:09 confirms that action:
However, during the process of rapidly restarting your program in Session 0, AlwaysUp is notified of another important event. Windows is shutting down. This is reported at 10:26:09 too:
And finally, a mere two seconds later, both the application and service stop.
So the only hiccup — as you mentioned — is the unnecessary, “phantom” restart that happens at 10:26:08. This is because AlwaysUp is not aware of the system shutdown at the time when it detects the application outage. AlwaysUp is just doing it’s job, trying to make sure that your application is always running, 24×7.
This is a classic race condition, where two events (“session closed” and “system shutting down” notifications) occur in an unpredictable order. When AlwaysUp is notified of the shutdown first, it won’t restart your application. However, when AlwaysUp learns of the session close first, it will restart your application. And unfortunately you never know which sequence/behavior you’re going to get. 馃檪
How to avoid application restarts during Windows ShutdownTo overcome the problem, configure AlwaysUp to wait for a few seconds before restarting your application. Make that change on the Restart tab.
For example, here we have instructed AlwaysUp to delay for 15 seconds:
That short pause should provide enough time for AlwaysUp to receive the Windows shutdown notification and exit gracefully — without a phantom restart.
Of course, this adjustment means that AlwaysUp will not restart your program immediately if it crashes. If that’s a problem (i.e. your application must achieve close to 100% uptime), you may want to reduce the delay. Even 5 seconds should be sufficient to sidestep the dreaded race condition.
You may also like...My OneDrive Windows Service Doesn't Work - Help!Q&A: How do I Enforce a Single Instance of my Python Script with AlwaysUp?Q & A: What Setting will Help AlwaysUp Start my App at Boot?